#!/bin/bash

. $OPENARS_HOME/common/functions.h

rep_dir=$REPOSITORY_PATH/scripts
if [ ! -d "$rep_dir" ]; then
    echo "not find repository dir"
    exit 1
fi

cd `dirname $0`
mkdir -p $OPENARS_HOME/logs/zabbix_manager $OPENARS_HOME/scripts/zabbix_manager/temp
MYLOG=$OPENARS_HOME/logs/zabbix_manager/install_zabbix_`date +%Y-%m-%d`.log
exec 2>>$MYLOG


list=`ls input/scripts2install`
if [ -z "$list" ]; then
    echo "not find scripts to install (path: input/scripts2install)"
    exit 1
fi

echo -e "\tscripts:\n$list"
question "Continue?" qres
if [ "$qres" != "Y" ]; then
    echo "exit"
    exit 0;
fi

#CONNECT=`cat ora_conn.conf`
dt=`date +%y-%m-%d_%H_%M`
CMDB=`echo $CONNECT_CMDB | awk -F"/" '{print $1}'`
{
echo "======================================"
echo "`basename $0` `date +'%Y.%m.%d %H:%M'`"
echo "======================================"  
for script_name in $list
    do
    sleep 1
    echo "install $script_name ..."
    cks_tmp=`cksum input/scripts2install/$script_name |awk '{print $1}'`
    servers=`$ORACLE_HOME/bin/sqlplus -s /NOLOG  <<END
	connect $CONNECT_ZABBIX
	SET ECHO OFF
	SET FEEDBACK OFF
	SET HEADING OFF
	set linesize 100
	set serveroutput on
	select h2.host from hosts_templates t,hosts h1, hosts h2, $CMDB.hosts c  where t.templateid=h1.hostid and t.hostid=h2.hostid and h1.host in (select template_name  from template_scripts where script_name like '%/etc/zabbix/%$script_name%') and h2.host=c.host and c.control='YES';
	exit;
    END`
    servers1=""
    for i in $@
    do
	echo $servers | sed 's/^/ /;s/$/ /' | grep " $i " > /dev/null;
	[ "$?" == 0 ] && servers1="$servers1 $i"
    done
    [ ! -z "$1" ] && servers=$servers1
    
    scr_cks=`$ORACLE_HOME/bin/sqlplus -s /NOLOG  <<END
	connect $CONNECT_ZABBIX
	SET ECHO OFF
	SET FEEDBACK OFF
	SET HEADING OFF
	set linesize 100
	set serveroutput on
	select crc from template_scripts where script_name like '%/etc/zabbix/%$script_name%';
	exit;
    END`
    scr_cks=`echo "$scr_cks" | uniq`
    scr_cks=`echo $scr_cks`

    pt=`$ORACLE_HOME/bin/sqlplus -s /NOLOG  <<END
	connect $CONNECT_ZABBIX
	SET ECHO OFF
	SET FEEDBACK OFF
	SET HEADING OFF
	set linesize 100
	set serveroutput on
	select script_name from template_scripts where script_name like '%/etc/zabbix/%$script_name%';
	exit;
    END`	
    pt=`echo "$pt" | uniq`

    pt=`echo $pt |sed s/'.*,'// |sed s/\$.//g | sed s/\ *$//`
    [ -z "$pt" ] && { echo -e "\terr: not find script $script_name in table \"template_scripts\""; echo -e "\tnot install!"; continue; }
    [ -z "$servers" ] && { echo -e "\terr: not find servers for script $script_name"; echo -e "\tnot install!"; continue; }
    [ -z "$cks_tmp" ] && { echo -e "\terr: not get checksum of $script_name in input directory"; echo -e "\tnot install!"; continue; } 
    [ -z "$scr_cks" ] && { echo -e "\terr: not get src in table template_scripts"; echo -e "\tnot install!"; continue; }
    [ "`echo $pt | wc -w`" -gt 1 -o "`echo $scr_cks | wc -w`" -gt 1 ] && { echo -e "\terr: double script in table template_scripts. Please correct."; echo -e "\tnot install!"; continue; }
    if [ "$cks_tmp" != "$scr_cks" ]; then
        echo -e "\terr: cheksum of $script_name is differ in input directory and table \"template_scripts\". Please correct."
	echo -e "\tnot install!"
        continue
    fi
    echo -e "\tservers:"
    echo -e "\t"$servers
    
    question "Continue?" qres
    if [ "$qres" != "Y" ]; then
	echo -e "\tnot install!"
	continue
    fi

    success_flag=""
    for server in $servers
    do
	echo -en "\t\t$server ... "
sftp $USER_ZABBIX@$server 1>temp/iscr_sftp.log 2>temp/iscr_sftp.err <<END
put input/scripts2install/$script_name $pt
chmod 755 $pt
END
	if [ `cat temp/iscr_sftp.err 2>/dev/null | grep -v "^|" | grep -v "^$" |  wc -l` -gt 1 ]; then
	    echo "ERR" 
	else
	    echo "OK"
	    success_flag="OK"
	fi
	cat temp/iscr_sftp.log temp/iscr_sftp.err >>$MYLOG
    done
    rm -f temp/iscr_sftp.log temp/iscr_sftp.err
        if [ "$success_flag" = "OK" ]; then
	    echo -en "\tmove to repository ... "
	    mv input/scripts2install/$script_name $rep_dir >>$MYLOG 2>&1
            if [ "$?" -eq 0 ]; then
		echo "OK"
	    else
		echo "ERR"
	    fi
            if [ -d "$rep_dir/.svn" ]; then
	        cd $rep_dir
	        svn status $script_name| grep "?" >/dev/null
	        [ "$?" -eq 0 ] && svn add $script_name
	        cd - > /dev/null
	    fi
												    
	    
	else
	    echo -e "\tnot install!"
	fi	
done

if [ -d "$rep_dir/.svn" ]; then
    echo -e "\nsvn commit ..."
    cd $rep_dir
    svn commit -m "`svn status`"
    sleep 1
    svn update
    cd - > /dev/null
fi

}| tee -a $MYLOG
